package ca.usherbrooke.view.properties;

import java.io.IOException;

import org.newdawn.slick.Animation;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;

public class EntityViewProperties {
	private static final String SPRITE_SHEET_PATH = "sprite_sheet_path_key";
	private static final String SPRITE_SHEET_ENTITY_WIDTH = "sprite_sheet_entity_width";
	private static final String SPRITE_SHEET_ENTITY_HEIGHT = "sprite_sheet_entity_height";
	private static final String DEFAULT_IMG = "default";

	private final PropertieBase properties;
	private final SpriteSheet characterSheet;
	private final int defaultId;

	public EntityViewProperties(String fileName) throws SlickException,
			IOException {
		properties = new PropertieBase(fileName);

		characterSheet = new SpriteSheet(properties.getPath(SPRITE_SHEET_PATH),
				properties.getIntegerValue(SPRITE_SHEET_ENTITY_WIDTH),
				properties.getIntegerValue(SPRITE_SHEET_ENTITY_HEIGHT));

		defaultId = properties.getIntegerValue(DEFAULT_IMG);
	}

	/**
	 * 
	 * @return the animation[] up, down, right, left
	 */
	protected Animation[] generateAnimation(int i) {
		int xPos = (i % 4) * 3;
		int yPos = (int) Math.floor(i / 4) * 4;

		Animation[] allAnimation = new Animation[4];
		allAnimation[0] = new Animation();
		allAnimation[0].setAutoUpdate(true);
		for (int frame = 0; frame < 3; frame++) {
			allAnimation[0].addFrame(
					characterSheet.getSprite(xPos + frame, yPos), 150);
		}

		allAnimation[1] = new Animation();
		allAnimation[1].setAutoUpdate(true);
		for (int frame = 0; frame < 3; frame++) {
			allAnimation[1].addFrame(
					characterSheet.getSprite(xPos + frame, yPos + 2), 150);
		}

		allAnimation[2] = new Animation();
		allAnimation[2].setAutoUpdate(true);
		for (int frame = 0; frame < 3; frame++) {
			allAnimation[2].addFrame(
					characterSheet.getSprite(xPos + frame, yPos + 1), 150);
		}

		allAnimation[3] = new Animation();
		allAnimation[3].setAutoUpdate(true);
		for (int frame = 0; frame < 3; frame++) {
			allAnimation[3].addFrame(
					characterSheet.getSprite(xPos + frame, yPos + 3), 150);
		}
		return allAnimation;
	}

	private Integer getId(String key) {
		return properties.getIntegerValue(key, defaultId);
	}

	public Animation[] getAnimation(String key) {
		return generateAnimation(getId(key));
	}
}
